package cn.tedu.review;
/*本类用于复写售票案例实现方案1*/
public class TestThread {
    public static void main(String[] args) {
        //5,创建多个自定义类对象作为多个线程对象,里面包含了我们的业务
        TicketThread t1 = new TicketThread();
        TicketThread t2 = new TicketThread();
        TicketThread t3 = new TicketThread();
        TicketThread t4 = new TicketThread();
        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }
}
//1,自定义多线程类
class TicketThread extends Thread{
    //3定义一个变量保存票数
    /*将票数设置成静态,静态资源被本类的所有对象共享,值只有一份,解决卖400张票的问题*/
    static int tickets=100;
    //static Object o= new Object();
   // static Dog d=new Dog();
    //2,重写父类run(),里面是我们自己的业务
    @Override
    public void run() {

        //4,循环卖票
        while (true){
            synchronized (TicketThread.class) {
                if (tickets>0){
                    //7,让线程休眠,增加线程状态切换的顺序与出错的概率
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //打印当前正在售票的线程名,并且票数-1
                    System.out.println(getName() + ":" + tickets--);
                }

                if (tickets <= 0) break;
            }
        }
    }
}
class Dog{

}